package com.example.controller;

import com.example.entity.Employee;
import com.example.query.EmpQueryParam;
import com.example.service.EmployeeService;
import com.example.util.PageResult;
import com.example.util.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

@Slf4j
@RequestMapping("/emps")
@RestController
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    @GetMapping("/list")
    public Result list() {
        log.info("查询部门列表数据");
        List<Employee> emps = employeeService.findAll();
        return Result.success(emps);
    }

    @GetMapping
    public Result page(EmpQueryParam empQueryParam) {
        log.info("查询员工信息，empQueryParam = {}", empQueryParam);
        PageResult pageResult = employeeService.page(empQueryParam);
        return Result.success(pageResult);
    }
    /**
     * 保存员工信息
     * @param employee
     * @return
     */
    @PostMapping
    public Result save(@RequestBody Employee employee) {
        log.info("请求参数employee = {}", employee);
        employeeService.save(employee);
        return Result.success();
    }


    @DeleteMapping
    public Result delete(@RequestParam List<Integer> ids) {
        log.info("需要批量删除的ids = {}", ids);
        employeeService.deleteByIds(ids);
        return Result.success();
    }


    @GetMapping("/{id}")
    public Result get(@PathVariable Integer id) {
        log.info("根据id = {} 进行查询", id);
        Employee employee = employeeService.getById(id);
        return Result.success(employee);
    }
    @PutMapping
    public Result update(@RequestBody Employee employee) {
        log.info("更新employee = {}", employee);
        employeeService.update(employee);
        return Result.success();
    }
}
